﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PagedList;
using LKVService;
using LKVMLMLib; 
using LKVMLMADMIN.Models;
namespace LKVMLMADMIN.Controllers
{
    public class OrderController : Controller
    {
        public int i=0;
        public LKVService.OrderService Orderservice = new LKVService.OrderService();
        public LKVService.User userService = new User();
        public ActionResult DoanhSo(int page = 1, string SearchType = "", string status = "", string Fromdate = "", string Todate = "", string Username = "", string message = "")
        {
            ViewBag.Title = "Danh sách đơn hàng";
            ViewBag.Msg = message;
            if (Session["User"] != null)
            {            
                DateTime fromdate;
                DateTime todate;
                int Stt = 0;
                if (Fromdate == "")
                    fromdate =new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
                else
                { 
                    fromdate = Convert.ToDateTime(Fromdate);
                    ViewBag.DateFrom = Fromdate;
                }
                if (Todate == "")
                    todate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1).AddDays(-1);
                else
                {
                    todate = Convert.ToDateTime(Todate);
                    ViewBag.Todate = Todate;
                }
                if (status != "")
                    Stt = Convert.ToInt32(status);
                var Tonghop = Orderservice.getListOrderWithCondition(SearchType, Username, Stt, fromdate.Date, todate.Date, (page - 1) * 100, 100);
                var ListOder = Tonghop.Item2;
                double? Sumamount = 0;
                if (ListOder != null)
                    Sumamount = ListOder.Sum(s => s.Amount);
                ViewBag.tongtien = Sumamount;
                int[] myArray = Enumerable.Repeat(-1, Tonghop.Item1).ToArray();
                ViewBag.DoanhSo = ListOder;
                return View(myArray.ToPagedList(page, 100));
            }
            else
                return RedirectToAction("Login", "Account");
        }
        public ActionResult Addorder(int Goi = 0, int User = 0, string Time = "", bool Check = false, string ID_Order = "", string Amount="" , int id=0)
        {
        if (Session["User"] != null)
        {
            float Tien =0;          
            if (id != 0)
            {
                var a =userService.findUserById(id);
                ViewBag.username = userService.getListUser().Where(w=>w.UserId==id).ToList();
                ViewBag.comboUserSelectedValue = a.UserName;
            }         
            else
            {        
                var ListUser= userService.getListUser().ToList();
                ViewBag.username = ListUser;
                foreach (var us in (List<UserProfile>)ListUser)
                {
                    ViewBag.comboUserSelectedValue = us.UserName.ToString();
                    break;
                }   
            }          
            var ListPackage = Orderservice.getListPackage();
            var Listpk = ListPackage.Select(x => new { value = x.ID, text = x.Price.ToString() }).ToList();
            Listpk.Insert(0, new { value = 0, text = "Chọn gói" });         
            if (ListPackage.Count()>0)
            foreach (var ck in ListPackage)
            {             
                    ViewBag.comboCKSelectedValue = ck.Price.ToString();
                    break;
            }                                
            DateTime TimeGoi;
            if(Time=="")
                TimeGoi = DateTime.Now;
            else
                TimeGoi = Convert.ToDateTime(Time);
            var Price = ListPackage;
            var Id = ListPackage.Select(s=>s.ID).ToList();
            ViewBag.price = Listpk;
            if (Goi == 0 && (Amount == "0" || Amount=="") )
            {
                return View();
            }
            if (ID_Order != "" && Amount != "0" && Amount != "")
            {
                //var btc = new BitcoinPayment.Controllers.BitCoinPaymentController();
                //var rate = btc.BitcointoUSD(1);
                //if (rate == 0)
                //    return View();
                Tien = float.Parse(Amount);
                var checkdk = Orderservice.addOrder(0, User, Tien, Goi, ID_Order, TimeGoi, false, true, 1,0,"",1);
                if (checkdk == true)
                    return RedirectToAction("ListUser", "User");
                else
                    return View();
            }
            else
                return View();                     
         
            
        }
            else
            return RedirectToAction("Login", "Account");
        }
        public ActionResult DeleteOrder(List<int?> id)
        {       
                Orderservice.DeleteOders(id);
                return RedirectToAction("DoanhSo", "Order");
        }
        public ActionResult Package(int page=1,int id=0)
        {
            if (Session["User"] != null)
            {
                if (id != 0)
                {
                    Orderservice.RemovePackage(id);
                }             
                var ListPackage = Orderservice.getListPackage();
                ViewBag.Package = ListPackage;
                int[] myArray = Enumerable.Repeat(-1, ListPackage.Count()).ToArray();
                return View(myArray.ToPagedList(page, 100));
            }
            else
                return RedirectToAction("Login", "Account");
        }
        public ActionResult AddPackage(string Name = "", string ID_Package = "", double Package_Price = 0, int Package_Point = 0)
        {          
            if(Name!="" && ID_Package!="" && Package_Price!=0 && Package_Point!=0)
            {           
                    var PackageName = Orderservice.findPackagebyPackageByName(Name);
                    if (PackageName == null)
                    {
                        Package newpackage = new Package();
                        newpackage.PackageName = Name;
                        newpackage.PackageCode = ID_Package;
                        newpackage.Price = Package_Price;
                        newpackage.PV = Package_Point;
                        Orderservice.AddPackage(newpackage);
                        return RedirectToAction("Package", "Order");
                    }
                    else
                        return View();             
                                          
            }
            else
            return View();
                 
        }
        [AllowAnonymous]
        public ActionResult PackageDetail(PackageModel model,int Pkid =0)
        {
            var PackageChoose = Orderservice.findPackagebyPackageById(Pkid);
            model.name = PackageChoose.PackageName;
            model.gia =Convert.ToString( PackageChoose.Price);
            model.diem =Convert.ToString( PackageChoose.PV);
            model.ma = PackageChoose.PackageCode;
            model.idpk = PackageChoose.ID;
            i = Pkid;
            return View(model);       
        }
        [HttpPost]
        [AllowAnonymous]
        public ActionResult PackageDetail( PackageModel model)
        {   
            if(model.name!=null)
            { 
            var PackageChoose = Orderservice.findPackagebyPackageById(model.idpk);         
            PackageChoose.PackageCode = model.ma;
            PackageChoose.PackageName = model.name;
            PackageChoose.Price = Convert.ToDouble(model.gia);
            PackageChoose.PV = Convert.ToInt32(model.diem);
            var pkupdate = Orderservice.EditPackage(PackageChoose); 
            if(pkupdate==1)
                return RedirectToAction("Package", "Order");
            else
                return View();
            }
            else
                return View();
        }
        public ActionResult ActiveOrder(int orderId = 0)
        {
            if (orderId == 0)
                return RedirectToAction("DoanhSo", "Order", new { message = "Order not exists" });
            var kq = Orderservice.activeOrder(orderId);
            if(kq == true)
                return RedirectToAction("DoanhSo", "Order", new { message = "Success" });
            else
                return RedirectToAction("DoanhSo", "Order", new { message = "Error" });
        }
       
    }
}
